package menu

import (
	"context"

	v1 "gitee.com/liangziyisheng/caas-platform-mc/api/menu/v1"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/consts"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/service"
	"gitee.com/liangziyisheng/caas-platform-mc/utility/utils"

	"github.com/gogf/gf/v2/errors/gerror"
)

func (c *ControllerV1) Add(ctx context.Context, req *v1.AddReq) (res *v1.AddRes, err error) {
	in := &v1.AddInput{}
	// 单type=1表示为应用,此时parent必需为空
	if req.Type == consts.MenuTypes[0].Value && req.Parent > 0 {
		return nil, gerror.NewCode(consts.AppMenuParentMustNull)
	}
	if req.Type != consts.MenuTypes[0].Value && req.Parent == 0 {
		return nil, gerror.NewCode(consts.AppMenuParentMustNotNull)
	}
	utils.CopyStruc(*req, in)
	out, err := service.Menu().Add(ctx, in)
	if err != nil {
		return nil, err
	}
	return &v1.AddRes{
		Id: out.Id,
	}, nil
}
